package org.csu.jpetstoreagain.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetstoreagain.domain.Account;
import org.csu.jpetstoreagain.domain.Cart;
import org.csu.jpetstoreagain.domain.CartItem;
import org.csu.jpetstoreagain.domain.Item;
import org.csu.jpetstoreagain.service.CartService;
import org.csu.jpetstoreagain.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Controller
//@RequestMapping("/cart")

public class CartFormController {

    @Autowired
    private CartService cartService;

    @Autowired
    private ItemService itemService;

    /**
     * 跳转到购物车
     *
     * @param req
     * @return
     */
    @GetMapping({"/cartForm","jsp/cart/cart"})
    public String cartForm(HttpServletRequest req, Model model) {
        HttpSession session = req.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        String userid=loginAccount.getUserid();
        if (loginAccount == null) {
            return "/jsp/account/signon";
        }
        List<Cart> list = cartService.listByUserId(userid);
        ArrayList<CartItem> cartItems = new ArrayList<>();
        for (Cart cart : list) {
            CartItem cartItem = new CartItem();
            Item item = itemService.getById(cart.getItemId());
            //zheli
            cartItem.setItem(item);
            int quantity = cart.getQuantity();
            cartItem.setQuantity(quantity);
            cartItem.setItemId(item.getItemid());
            BigDecimal listprice = item.getListprice();
            BigDecimal total =  listprice.multiply(BigDecimal.valueOf(quantity));
            cartItem.setTotal(total);
            //cartItem.setInStock();
            cartItems.add(cartItem);
        }
        model.addAttribute("allCartItems",cartItems);

        //添加一个已加入购物车的商品数量以判断是否可以确认下单
        int num = cartItems.size();
        model.addAttribute("numOfItems",num);
        return "jsp/cart/cart";
    }


    /**
     * 添加到购物车
     *
     * @param workingItemId workingItemId
     * @return String
     */
    @GetMapping("/addItemToCart")
    public String addItemToCart(HttpServletRequest request,@RequestParam("workingItemId")String workingItemId) {
        HttpSession session = request.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        if (loginAccount == null) {
            return "/jsp/account/signon";
        }
        String userid = loginAccount.getUserid();
        cartService.addItemToCart(userid, workingItemId);
        return "redirect:/jsp/cart/cart";
    }

    /**
     * 从购物车移除
     */
    @GetMapping("/removeCartItem")
    public String removeCartItem(HttpServletRequest request,@RequestParam("itemid") String itemid) {
        HttpSession session = request.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        if (loginAccount == null) {
            return "/jsp/account/signon";
        }
        String userid = loginAccount.getUserid();
        cartService.removeItem(userid, itemid);
        return "redirect:/jsp/cart/cart";
    }


    /**
     * 清空购物车
     */
    @GetMapping("/clearCart")
    public String clearCart(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        if (loginAccount == null){
            return "/jsp/account/signon";
        }
        String userid = loginAccount.getUserid();
        cartService.clearCart(userid);
        return "redirect:/jsp/cart/cart";
    }

    /**
     * 更新购物车
     * @param cart
     */
    @PostMapping("/updateCart")
    public String updateCart(Cart cart) {
        cartService.updateCart(cart);
        return "redirect:/jsp/cart/cart";
    }


}
